/**
 * \file valarray_operators.hpp
 * 
 * \section COPYRIGHT
 *
 * Basis: A 3D Mathematics Library
 *
 * ---------------------------------------------------------------------
 *
 * Copyright (c) 2010, Don Olmstead
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *  3. Neither the name of organization nor the names of its contributors may be
 *     used to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BASIS_DETAIL_VALARRAY_OPERATORS_HPP_INCLUDED
#define BASIS_DETAIL_VALARRAY_OPERATORS_HPP_INCLUDED

#include <basis/detail/traits.hpp>
#include <basis/detail/scalar_storage.hpp>

namespace basis { namespace detail
{

	//------------------------------------------------------------
	// Addition
	//------------------------------------------------------------

	/**
	 * Addition operator for the valarray.
	 *
	 * \tparam T The type held in the valarray.
	 * \tparam Op1 The first operand.
	 * \tparam Op2 The second operand.
	 */
	template <typename T, typename Op1, typename Op2>
	class op_addition
	{
		public:

			/// Unsigned integral type
			typedef std::size_t size_type;

			/**
			 * Initializes an instance of the op_add class.
			 *
			 * \param lhs The left hand side of the expression.
			 * \param rhs The right hand side of the expression.
			 */
			op_addition(const Op1& lhs, const Op2& rhs)
				: _lhs(lhs)
				, _rhs(rhs)
			{ }

			/**
			 * Array operator for the op_add class.
			 *
			 * \param i The index of the operation.
			 * \returns The sum of the operation.
			 */
			inline T operator[] (size_type i) const
			{
				return _lhs[i] + _rhs[i];
			}

			/**
			 * Retrieves te size of the op_add.
			 *
			 * \returns The size of the op_add.
			 */
			inline size_type size() const
			{
				// assert

				return _lhs.size() != 0 ? _lhs.size() : _rhs.size();
			}

		private:

			/// The value of the left hand side operand
			typename traits<Op1>::const_reference _lhs;
			/// The value of the right hand side operand
			typename traits<Op2>::const_reference _rhs;

	} ;

	template <typename T, typename Rep1, typename Rep2>
	valarray<T, op_addition<T, Rep1, Rep2> > operator+ (const valarray<T, Rep1>& lhs, const valarray<T, Rep2>& rhs)
	{
		return valarray<T, op_addition<T, Rep1, Rep2> >(op_addition<T, Rep1, Rep2>(lhs.rep(), rhs.rep()));
	}

	//------------------------------------------------------------
	// Subtraction
	//------------------------------------------------------------

	/**
	 * Subtraction operator for the valarray.
	 *
	 * \tparam T The type held in the valarray.
	 * \tparam Op1 The first operand.
	 * \tparam Op2 The second operand.
	 */
	template <typename T, typename Op1, typename Op2>
	class op_subtraction
	{
		public:

			/// Unsigned integral type
			typedef std::size_t size_type;

			/**
			 * Initializes an instance of the op_add class.
			 *
			 * \param lhs The left hand side of the expression.
			 * \param rhs The right hand side of the expression.
			 */
			op_subtraction(const Op1& lhs, const Op2& rhs)
				: _lhs(lhs)
				, _rhs(rhs)
			{ }

			/**
			 * Array operator for the op_add class.
			 *
			 * \param i The index of the operation.
			 * \returns The sum of the operation.
			 */
			inline T operator[] (size_type i) const
			{
				return _lhs[i] - _rhs[i];
			}

			/**
			 * Retrieves te size of the op_add.
			 *
			 * \returns The size of the op_add.
			 */
			inline size_type size() const
			{
				// assert

				return _lhs.size() != 0 ? _lhs.size() : _rhs.size();
			}

		private:

			/// The value of the left hand side operand
			typename traits<Op1>::const_reference _lhs;
			/// The value of the right hand side operand
			typename traits<Op2>::const_reference _rhs;

	} ;

	template <typename T, typename Rep1, typename Rep2>
	valarray<T, op_subtraction<T, Rep1, Rep2> > operator- (const valarray<T, Rep1>& lhs, const valarray<T, Rep2>& rhs)
	{
		return valarray<T, op_subtraction<T, Rep1, Rep2> >(op_subtraction<T, Rep1, Rep2>(lhs.rep(), rhs.rep()));
	}

	//------------------------------------------------------------
	// Multiply
	//------------------------------------------------------------

	/**
	 * Multiplication operator for the valarray.
	 *
	 * \tparam T The type held in the valarray.
	 * \tparam Op1 The first operand.
	 * \tparam Op2 The second operand.
	 */
	template <typename T, typename Op1, typename Op2>
	class op_multiplication
	{
		public:

			/// Unsigned integral type
			typedef std::size_t size_type;

			/**
			 * Initializes an instance of the op_mult class.
			 *
			 * \param lhs The left hand side of the expression.
			 * \param rhs The right hand side of the expression.
			 */
			op_multiplication(const Op1& lhs, const Op2& rhs)
				: _lhs(lhs)
				, _rhs(rhs)
			{ }

			/**
			 * Array operator for the op_add class.
			 *
			 * \param i The index of the operation.
			 * \returns The sum of the operation.
			 */
			inline T operator[] (size_type i) const
			{
				return _lhs[i] * _rhs[i];
			}

			/**
			 * Retrieves te size of the op_add.
			 *
			 * \returns The size of the op_add.
			 */
			inline size_type size() const
			{
				// assert

				return _lhs.size() != 0 ? _lhs.size() : _rhs.size();
			}

		private:

			/// The value of the left hand side operand
			typename traits<Op1>::const_reference _lhs;
			/// The value of the right hand side operand
			typename traits<Op2>::const_reference _rhs;

	} ;

	template <typename T, typename Rep1, typename Rep2>
	valarray<T, op_multiplication<T, Rep1, Rep2> > operator* (const valarray<T, Rep1>& lhs, const valarray<T, Rep2>& rhs)
	{
		return valarray<T, op_multiplication<T, Rep1, Rep2> >(op_multiplication<T, Rep1, Rep2>(lhs.rep(), rhs.rep()));
	}

	template <typename T, typename Rep2>
	valarray<T, op_multiplication<T, scalar_storage<T>, Rep2> > operator* (const T& lhs, const valarray<T, Rep2>& rhs)
	{
		return valarray<T, op_multiplication<T, scalar_storage<T>, Rep2> >(op_multiplication<T, scalar_storage<T>, Rep2>(scalar_storage<T>(lhs), rhs.rep()));
	}

	template <typename T, typename Rep1>
	valarray<T, op_multiplication<T, Rep1, scalar_storage<T> > > operator* (const valarray<T, Rep1>& lhs, const T& rhs)
	{
		return valarray<T, op_multiplication<T, Rep1, scalar_storage<T> > >(op_multiplication<T, Rep1, scalar_storage<T> >(lhs.rep(), scalar_storage<T>(rhs)));
	}

	//------------------------------------------------------------
	// Division
	//------------------------------------------------------------

	/**
	 * Division operator for the valarray.
	 *
	 * \tparam T The type held in the valarray.
	 * \tparam Op1 The first operand.
	 * \tparam Op2 The second operand.
	 */
	template <typename T, typename Op1, typename Op2>
	class op_division
	{
		public:

			/// Unsigned integral type
			typedef std::size_t size_type;

			/**
			 * Initializes an instance of the op_mult class.
			 *
			 * \param lhs The left hand side of the expression.
			 * \param rhs The right hand side of the expression.
			 */
			op_division(const Op1& lhs, const Op2& rhs)
				: _lhs(lhs)
				, _rhs(rhs)
			{ }

			/**
			 * Array operator for the op_add class.
			 *
			 * \param i The index of the operation.
			 * \returns The sum of the operation.
			 */
			inline T operator[] (size_type i) const
			{
				return _lhs[i] / _rhs[i];
			}

			/**
			 * Retrieves te size of the op_add.
			 *
			 * \returns The size of the op_add.
			 */
			inline size_type size() const
			{
				// assert

				return _lhs.size() != 0 ? _lhs.size() : _rhs.size();
			}

		private:

			/// The value of the left hand side operand
			typename traits<Op1>::const_reference _lhs;
			/// The value of the right hand side operand
			typename traits<Op2>::const_reference _rhs;

	} ;

	template <typename T, typename Rep1, typename Rep2>
	valarray<T, op_division<T, Rep1, Rep2> > operator/ (const valarray<T, Rep1>& lhs, const valarray<T, Rep2>& rhs)
	{
		return valarray<T, op_division<T, Rep1, Rep2> >(op_division<T, Rep1, Rep2>(lhs.rep(), rhs.rep()));
	}

	template <typename T, typename Rep2>
	valarray<T, op_division<T, scalar_storage<T>, Rep2> > operator/ (const T& lhs, const valarray<T, Rep2>& rhs)
	{
		return valarray<T, op_division<T, scalar_storage<T>, R2> >(op_division<T, scalar_storage<T>, Rep2>(scalar<T>(lhs), rhs.rep()));
	}

} } // end namespace basis::detail

#endif // end BASIS_DETAIL_VALARRAY_OPERATORS_HPP_INCLUDED
